package com.ccx.coachsite.util;


import java.io.File;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.widget.Toast;

import com.ccx.coachsite.R;

public class ApplicationUtil {
	
	/**
	 * 获取已安装应用的大小
	 * @param pkgName
	 * @return
	 */
	public static long getApplicationSize(Context ctx,String pkgName)
	{
		PackageManager pm=ctx.getPackageManager();
		try {
			ApplicationInfo appInfo=pm.getApplicationInfo(pkgName, 0);
			String pkgPath=appInfo.publicSourceDir;
			File file=new File(pkgPath);
			if(file.exists())
				return file.length();
		} catch (Exception e) {
		}
		return 0;
	}
	
	/**
	 * 获取已安装应用的大小
	 * @param pkgName
	 * @return
	 */
	public static long getApplicationSize(ApplicationInfo appInfo)
	{
		try {
			String pkgPath=appInfo.publicSourceDir;
			File file=new File(pkgPath);
			if(file.exists())
				return file.length();
		} catch (Exception e) {
		}
		return 0;
	}
	
	/**
	 * 是不是系统应用
	 * @param ctx
	 * @param pkgName
	 * @return
	 */
	public static boolean isSystemApplication(Context ctx,String pkgName)
	{
		PackageManager pm=ctx.getPackageManager();
		try {
			ApplicationInfo appInfo=pm.getApplicationInfo(pkgName, 0);
			//是否是系统进程
			if((appInfo.flags&ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)!=0)
				return true;
			else if((appInfo.flags&ApplicationInfo.FLAG_SYSTEM)!=0)
				return true;
		} catch (Exception e) {
		}
		return false;
	}
	
	/**
	 * 是不是系统应用
	 * @param ctx
	 * @param pkgName
	 * @return
	 */
	public static boolean isSystemApplication(ApplicationInfo appInfo)
	{
		if(appInfo==null)
			return false;
		//是否是系统应用
		if((appInfo.flags&ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)!=0)
			return true;
		else if((appInfo.flags&ApplicationInfo.FLAG_SYSTEM)!=0)
			return true;
			
		return false;
	}
	
	/**
	 * 安装应用
	 * @param wpVO
	 */
	public static void installApp(Context ctx,String apkPath)
	{
		File file=new File(apkPath);
		//安装文件不存在
		if(!file.exists())
		{
			Toast.makeText(ctx, ctx.getString(R.string.common_no_file), Toast.LENGTH_SHORT).show();
			return;
		}
		
		//判断签名的类
		SignedReader reader = new SignedReader();
		//获取安装包中的应用 的包名
		String packageName = reader.getPackageName(ctx, file);
		boolean re = true;
		
		//机子是否已安装该软件，如果已安装需要判断签名是否一致
		PackageManager pm = ctx.getPackageManager();
		ApplicationInfo appInfo=null;
		try {
			appInfo=pm.getApplicationInfo(packageName, 0);
		} catch (Exception e) {
		}
		
		if(appInfo==null) 
			re=true;
		else
			//比较签名是否一致
			re = reader.compareSigned(ctx, packageName, file.getAbsolutePath());
		
		
		if(!re){
			//签名不一致,需先卸载再安装
			Toast.makeText(ctx, R.string.common_signal_conflict, Toast.LENGTH_LONG).show();
			return ;
		}
		
		Uri uri = Uri.fromFile(file); 
		Intent intent = new Intent(Intent.ACTION_VIEW);
		intent.setDataAndType(uri, "application/vnd.android.package-archive" ); 
		ctx.startActivity(intent);
	}
	
	/**
	 * 卸载应用
	 * @param wpVO
	 */
	public static void uninstallApp(Context ctx,String pkgName)
	{
		Uri packageURI = Uri.parse("package:"+pkgName);      
		Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
		ctx.startActivity(uninstallIntent);
	}

}
